//
//  WKWindow.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/2/27.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation


class WKWindow: NSWindow {
    
    override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
        super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: flag)
        self.reStitlebarView()
    }
    
    func frame(for size: NSSize) -> NSRect {
        var newFrame: NSRect = frame
        newFrame.origin.x += (newFrame.size.width - size.width) / 2
        newFrame.origin.y += newFrame.size.height - size.height
        newFrame.size = size
        print(newFrame)
        return newFrame
    }
    
    override func setFrame(_ frameRect: NSRect, display flag: Bool) {
        let rect = self.inLiveResize ? self.frame(for: frameRect.size):frameRect
        super.setFrame(rect, display: flag)
    }
    
    
    func reStitlebarView() {
        //暂时解决
        for subview in (self.sTitlebarView?.subviews.enumerated())! {
            if subview.element.isKind(of: NSClassFromString("NSTitlebarView")!) == true {
                for btn in subview.element.subviews.enumerated(){
                    btn.element.snp.updateConstraints { (make) in
                        make.top.equalTo(15)
                    }
                }
            }
        }
      
        self.sTitlebarView?.snp.makeConstraints({ (make) in
            make.height.equalTo(kNavBarHeight)
            make.left.right.top.equalToSuperview()
        })
      
        //添加自定义views
        self.addCustemViews()
    }
    
    func addCustemViews() {
        self.sTitlebarView?.addSubview(sTitle)
        sTitle.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.centerX.equalToSuperview()
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        
        self.sTitlebarView?.addSubview(sLine)
        sLine.snp.makeConstraints { (make) in
            make.height.equalTo(1)
            make.bottom.equalToSuperview()
            make.width.centerX.equalToSuperview()
        }
    }
    
    //获取 titlebarView
    lazy var sTitlebarView: NSView? = {
        let themeView = self.contentView?.superview
        let titleView  = themeView?.subviews[1]
        titleView?.wantsLayer = true
        titleView?.layer?.backgroundColor = color_gray(245).cgColor
        return titleView
    }()
    
    lazy var sTitle: NSLabel = {
        let lab = NSLabel.label(title: "设置".localized(), textColor:  NSColor.black, font: NSFont.systemFont(ofSize: 16))
        lab.alignment = .center
        lab.isHidden = false
        lab.sizeToFit()
        return lab
    }()
    
    lazy var sLine: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(220).cgColor
        return view
    }()
    
}

